package com.seeyon.apps.taxFund.api;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.seeyon.apps.commonExt.util.APIBaseOperation;
import com.seeyon.cap4.form.exception.FormException;
import com.seeyon.ctp.common.AppContext;

import java.util.Optional;

public enum ShuiJinEnum implements APIBaseOperation {


    USER_SYNC("用户同步","/openapi/userInfoReceive"),
    LOGIN_AUTH("单点登陆","/pxocr/singlelogin"),
    PRODUCT_QUERY("商品信息查询接口","/openapi/sale/product"),
    INVOICE_SAVE("发票入账信息同步","/openapi/income/voucher"),
    ORDER_INFO("红蓝业务单据接口","/openapi/orderInfo"),
    INVOICE_VALID("乐企发票查验","/openapi/allElectricInfo");

    private final String desc;

    private final String url;

    ShuiJinEnum(String desc, String url) {
        this.url = url;
        this.desc = desc;
    }

    @Override
    public String serverName() {
        return "税金系统";
    }
    @Override
    public String baseUrl() {
        return Optional.ofNullable(AppContext.getSystemProperty("shuijin.api.url"))
                .orElseThrow(() -> new FormException("请配置税金系统的服务地址"));
    }

    @Override
    public String url() {
        return baseUrl() + this.url;
    }

    @Override
    public String desc() {
        return String.format("%s-%s", serverName(), desc);
    }

    @Override
    public void success(String resStr) {
        BaseResponse bean = JSONUtil.toBean(resStr, BaseResponse.class);
        Assert.notNull(bean, "【{}】返回结果为空", desc());
        Assert.notNull(bean.getCode(), "【{}】返回状态码为空", desc());
        if (StrUtil.equalsAny(bean.getCode(), "0000", "9000", "200")) {
            return;
        }
        if (StrUtil.isNotBlank(bean.getMsg())) {
            throw new FormException(StrUtil.format("【{}】服务异常【{}】", this.desc, bean.getMsg()));
        }
        if (StrUtil.isNotBlank(bean.getMessage())) {
            throw new FormException(StrUtil.format("【{}】服务异常【{}】", this.desc, bean.getMessage()));
        }
        throw new FormException(StrUtil.format("【{}】服务异常", this.desc));
    }
}
